package com.steptime.thread.reentrantlock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 演示多线程预定电影院座位
 */
public class ChinemaBookSeat {
    private static ReentrantLock lock = new ReentrantLock();

    public static void main(String[] args) {
        new Thread(()->bookSeat()).start();
        new Thread(()->bookSeat()).start();
        new Thread(()->bookSeat()).start();
        new Thread(()->bookSeat()).start();
    }
    private static void bookSeat(){
        lock.lock();
         try{
             System.out.println(Thread.currentThread().getName()+"开始预定电影票");
             try {
                 Thread.sleep(1000);
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
             System.out.println(Thread.currentThread().getName()+"完成预定电影票");

         }finally {
             lock.unlock();
         }
    }
}
